Forked from UraraReika/jet-woo-builder-ajax-add-to-cart-with-variation-swatches.php
Created
October 17, 2022 14:24
-
-
Save LesykJulia/170347a62714dc4e3325c5ef34aadc4e to your computer and use it in GitHub Desktop.
Adding AJAX support for adding to cart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'jet-woo-builder/template-functions/product-add-to-cart-settings', 'wvs_pro_archive_variation_button_args', 999, 2 ); | |
function wvs_pro_archive_variation_button_args( $args ) { | |
global $product; | |
$product = wc_get_product(); | |
$ajax_add_to_cart_enabled = 'yes' === get_option( 'woocommerce_enable_ajax_add_to_cart' ); | |
if ( 'variable' === $product->get_type() ) { | |
$classname = WC_Product_Factory::get_classname_from_product_type( 'simple' ); | |
$as_single_product = new $classname( $product->get_id() ); | |
$args['attributes']['data-add-to-cart'] = htmlspecialchars( wp_kses_post( apply_filters( 'woo_variation_swatches_archive_add_to_cart_text', apply_filters( 'woo_variation_swatches_archive_add_to_cart_button_text', apply_filters( 'add_to_cart_text', $as_single_product->add_to_cart_text() ), $product ), $product, $as_single_product ) ) ); | |
$args['attributes']['data-select-options'] = htmlspecialchars( wp_kses_post( apply_filters( 'woo_variation_swatches_archive_add_to_cart_select_options', apply_filters( 'woo_variation_swatches_archive_add_to_cart_button_text', apply_filters( 'variable_add_to_cart_text', $product->add_to_cart_text() ), $product ), $product, $as_single_product ) ) ); | |
$args['class'] .= ' wvs_add_to_cart_button wvs-add-to-cart-button'; | |
if ( $ajax_add_to_cart_enabled ) { | |
$args['class'] .= ' wvs_ajax_add_to_cart'; | |
} else { | |
$args[ 'attributes' ][ 'data-product_permalink' ] = $product->add_to_cart_url(); | |
$args['attributes']['data-add_to_cart_url'] = $product->is_purchasable() && $product->is_in_stock() ? wvs_pro_get_current_url() : get_permalink( $product->get_id() ); | |
} | |
$args[ 'attributes' ][ 'data-variation_id' ] = ""; | |
$args[ 'attributes' ][ 'data-variation' ] = ""; | |
} | |
return $args; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment