2013-10-25 136 views
0

我想在wordpress中的POST提交按鈕上調用function.php中的自定義函數。 貝婁你可以看到,當我刪除它的聲明函數調用工作的完美,但與if語句它不工作時,我發佈我在if語句中達成,但無法調用該函數。使用woocommerce wordpress在function.php中調用自定義函數

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

    } 
+0

嘗試移動'customcart()'frunction定義了'if'塊的,看看會發生什麼。 – geomagas

+0

它工作了,如果塊,但我想調用函數的一個事件,這就是爲什麼我需要,如果塊。 @geomagas –

+0

它沒有區別,是嗎? – geomagas

回答

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')))); 

    } 

    } 

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