最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - I want to add custom add to cart link

programmeradmin2浏览0评论
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I am trying to add custom add to cart link. I am getting product details from product slug. And trying to add, ADD to cart link by product id but no luck so far. Any help will be highly appreciated.

if(!empty($tracks[ $k ][ 'buy_link_a' ])){
            list($hash, $slug) = explode("product/",$tracks[ $k ][ 'buy_link_a' ]);
            $product_obj = get_page_by_path( $slug, OBJECT, 'product' ); // this code help me to get product detials by slug
           $id=$product_obj->ID; // and here I am getting product id 
           do_action( 'woocommerce_' . $product->product_type . '_add_to_cart'  );
       }
Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 5 years ago.

Improve this question

I am trying to add custom add to cart link. I am getting product details from product slug. And trying to add, ADD to cart link by product id but no luck so far. Any help will be highly appreciated.

if(!empty($tracks[ $k ][ 'buy_link_a' ])){
            list($hash, $slug) = explode("product/",$tracks[ $k ][ 'buy_link_a' ]);
            $product_obj = get_page_by_path( $slug, OBJECT, 'product' ); // this code help me to get product detials by slug
           $id=$product_obj->ID; // and here I am getting product id 
           do_action( 'woocommerce_' . $product->product_type . '_add_to_cart'  );
       }
Share Improve this question edited Sep 20, 2018 at 11:45 Varsha Dhadge 3411 gold badge2 silver badges14 bronze badges asked Aug 15, 2017 at 14:40 sanjaysanjay 111 silver badge2 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Your problem is that get_page_by_path() returns a WP_Post object, not a WC_Product object. You need a WC_Product object, because that's the object that contains the product_type property. You can get a WC_Product from a WP_Post via the wc_get_product() function:

$post_obj = get_page_by_path( $slug, OBJECT, 'product' ); // this code help me to get product detials by slug
$product_obj = wc_get_product( $page_obj );

Your code still won't work though, because the woocommerce_simple_add_to_cart hook relies on the global $product variable, which won't be set to the product you're querying here. You could set it yourself but I'm not sure of the side effects of that. The safest way in my view would be to use the [add_to_cart] shortcode:

if(!empty($tracks[ $k ][ 'buy_link_a' ])){
    list($hash, $slug) = explode("product/",$tracks[ $k ][ 'buy_link_a' ]);
    $post_obj = get_page_by_path( $slug, OBJECT, 'product' ); // this code help me to get product detials by slug
    $product_obj = wc_get_product( $page_obj );
    echo do_shortcode( '[add_to_cart id="' . $product_obj->get_id() . '" show_price="false"]' );
}

If you just want the URL, use the [add_to_cart_url] shortcode:

echo do_shortcode( '[add_to_cart_url id="' . $product_obj->get_id() . '"]' );

Also note that I used the get_type() method instead of the product_type property, since that property has been deprecated in favour of the method.

Please find your answer below

if(!empty($tracks[ $k ][ 'buy_link_a' ])){
            list($hash, $slug) = explode("product/",$tracks[ $k ][ 'buy_link_a' ]);
            $product_obj = get_page_by_path( $slug, OBJECT, 'product' ); // this code help me to get product detials by slug
           $id=$product_obj->ID; // and here I am getting product id 

       }
<a href="<?php get_bloginfo('url');?>/home-testing/?add-to-cart=<?php $product_obj->ID;?>" class="single_add_to_cart_button button alt">Add to cart</a>
发布评论

评论列表(0)

  1. 暂无评论