首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用woocommerce wordpress调用function.php中的自定义函数

使用woocommerce wordpress调用function.php中的自定义函数
EN

Stack Overflow用户
提问于 2013-10-25 06:32:50
回答 2查看 5.2K关注 0票数 0

我试图在function.php中调用一个自定义函数,在wordpress中的POST提交按钮。贝娄,你可以看到if语句,当我删除它时,函数调用工作是完美的,但是使用if语句,它不是工作,当我是post时,我在if语句中被联系到,但不能调用该函数。

代码语言:javascript
复制
if (isset($_POST['addcustomcarts']))

    {
        echo("if");
    add_filter('woocommerce_before_cart', 'customcart');
    //global $woocommerce;

    function customcart() {
    global $woocommerce;
    $my_post = array(
      'post_title'    => 'My post',
      'post_content'  => 'This is my post.',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     =>'product'
    );
    echo"sdafsdf";
    // Insert the post into the database
     $product_ID=wp_insert_post( $my_post );

     add_post_meta($product_ID, '_regular_price', 100, $unique);
     add_post_meta($product_ID, '_price', 100, $unique);
      add_post_meta($product_ID, '_stock_status', 'instock', $unique);



    $woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );


    //exit;
    //header("Location: http://www.mydomain.com");exit();
    wp_redirect(".home_url('cart').");
    //wp_redirect(home_url());

    //global $wpdb;
    //$wpdb->query
    //exit;
    //exit;
    }

    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-26 06:43:49

我已经解决了这个问题,用add_action('init', 'customcart');代替add_filter('woocommerce_before_cart', 'customcart');

代码语言:javascript
复制
add_action('init', 'customcart');

function customcart() {

  if (isset($_POST["addcustomcarts"])) {

    global $woocommerce;

    $my_post = array(
      'post_title'    => 'My post',
      'post_content'  => 'This is my post.',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     =>'product'
    );

    // Insert the post into the database
    $product_ID = wp_insert_post( $my_post );

    if ( $product_ID ){
      add_post_meta($product_ID, '_regular_price', 100 );
      add_post_meta($product_ID, '_price', 100 );
      add_post_meta($product_ID, '_stock_status', 'instock' );

      //Getting error on this line.
      $woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );

      exit( wp_redirect( get_permalink( woocommerce_get_page_id( 'cart' ) ) ) );

    }

  }

}
票数 0
EN

Stack Overflow用户

发布于 2013-10-25 14:25:11

您需要从if语句中删除整个函数,并且只调用该操作。如下所示:

代码语言:javascript
复制
if (isset($_POST['addcustomcarts']))

    {
        echo("if");
    add_filter('woocommerce_before_cart', 'customcart');
    //global $woocommerce;


    }


function customcart() {
    global $woocommerce;
    $my_post = array(
      'post_title'    => 'My post',
      'post_content'  => 'This is my post.',
      'post_status'   => 'publish',
      'post_author'   => 1,
      'post_type'     =>'product'
    );
    echo"sdafsdf";
    // Insert the post into the database
     $product_ID=wp_insert_post( $my_post );

     add_post_meta($product_ID, '_regular_price', 100, $unique);
     add_post_meta($product_ID, '_price', 100, $unique);
      add_post_meta($product_ID, '_stock_status', 'instock', $unique);



    $woocommerce->cart->add_to_cart( $product_ID, $quantity=1 );


    //exit;
    //header("Location: http://www.mydomain.com");exit();
    wp_redirect(".home_url('cart').");
    //wp_redirect(home_url());

    //global $wpdb;
    //$wpdb->query
    //exit;
    //exit;
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19582664

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档