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

时间:2013-10-25 06:32:51

标签: wordpress woocommerce

我试图在wordpress中的POST提交按钮上调用function.php中的自定义函数。 Bellow你可以看到if语句当我删除它时函数调用工作完全但是if语句它不起作用当我发布我在if语句中到达但不能调用该函数。

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;
    }

    }

2 个答案:

答案 0 :(得分:1)

您需要从if语句中删除整个函数,并且只调用该操作。像这样:

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 :(得分:0)

我使用add_action('init', 'customcart');代替add_filter('woocommerce_before_cart', 'customcart');

来解决问题
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' ) ) ) );

    }

  }

}